using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class Lesson4__exercise : MonoBehaviour
{
    public float zOffset = 2;
    public float yOffset = 0.5f;
    public Transform camera;
    PlayerInput input;
    private Rigidbody body;
    private Vector3 dir;

    void Start()
    {
        body = this.GetComponent<Rigidbody>();
        input = this.GetComponent<PlayerInput>();
        input.onActionTriggered += onActionTrigger;
    }

    public void OnMove(InputAction.CallbackContext context)
    {
        dir = context.ReadValue<Vector2>();
        dir.z = dir.y;
        dir.y = 0;
    }

    public void onActionTrigger(InputAction.CallbackContext context)
    {
        Debug.LogFormat("[onActionTrigger] name:{0}", context.action.name);
        switch (context.action.name)
        {
            case "Move":
                OnMove(context);
                break;
        }
    }

    void Update()
    {
        body.AddForce(dir * 10);
        camera.position = Vector3.Lerp(camera.position, this.transform.position - new Vector3(0, -7 * yOffset, 4 * zOffset), Time.deltaTime);
    }
}
